import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class Leetcode821 {
    public int[] shortestToChar(String s, char c) {
        int n = s.length();
        int firstIndex = s.indexOf(c);
        int[] res = new int[n];

        //处理首个c之前的
        for (int index = firstIndex; index >= 0; index--) {
            res[index] = firstIndex - index;
        }

        char[] chars = s.toCharArray();
        int index1 = 0;
        //处理首个c之后的
        for (int i = firstIndex + 1; i < n; i++) {
            if (chars[i] == c) {
                index1 = 0;
            } else {
                index1++;
            }

            res[i] = index1;
        }

        int lastIndex = s.lastIndexOf(c);

        int index2 = 0;
        //处理首位c之间的要取近的
        for (int i = lastIndex - 1; i > firstIndex; i--) {
            if (chars[i] == c) {
                index2 = 0;
            } else {
                index2++;
            }

            res[i] = Math.min(index2, res[i]);
        }

        return res;
    }
}

